#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "ohos_init.h"
#include "cmsis_os2.h"

#include "uart.h"
#include "oled.h"
#include "ctrl_task.h"
#include "tcp_client.h"
#include "device_app.h"



uint8_t receive_buff[32] = {0};
uint8_t send_buff[32] = {0};
uint8_t uart_receive_state = 
// uint8_t *send_buff_p = send_buff;

extern device_data_t device_data;
extern ctrl_data_t ctrl_data;
extern server_data_t server_data;

static void blu_reset(void);
static void blu_base(void);
static void blu_9_access(void);

static void function_b_user(void);
static void function_b_user_t(void);


void uart_task(void *arg)
{
    (void) arg;
    bzero(receive_buff, sizeof(receive_buff));
    while(1)
    {
        UartRead(WIFI_IOT_UART_IDX_2, receive_buff, sizeof(receive_buff));
        //
        switch(receive_buff[1])
        {
            case 0x42 : function_b_user(); break;    //自定义的数据协议//每秒一个心跳包，没有心跳包的就是离线的
            case 0x40 : blu_base(); break;    //设备入网等基本功能
            case 0x43 : blu_reset(); break;    //恢复出厂设置与重启设备////设备入网错误入口
            default : break;
        }
        bzero(receive_buff, sizeof(receive_buff));
        osDelay(100);   //1s
    }
}

void uart_task_creat(void)
{
    osThreadAttr_t attr;

    attr.name       = "uart_task";
    attr.attr_bits  = 0U;
    attr.cb_mem     = NULL;
    attr.cb_size    = 0U;
    attr.stack_mem  = NULL;
    attr.stack_size = 2048;
    attr.priority   = osPriorityNormal2;

    if (osThreadNew(uart_task, NULL, &attr) == NULL)
    {
        printf("[uart_task_creat] Falied to create uart_task!\n");
    }
}

APP_FEATURE_INIT(uart_task_creat);


static void blu_reset(void)
{
    switch(receive_buff[2])
    {
        case 0x15 : OLED_Clear_Line(1); uint8_t refactory_ok[] = {"factory ok"}; OLED_Show_String(0, 1, 8, refactory_ok); break;
        case 0x14 : OLED_Clear_Line(1); uint8_t reset_ok[] = {"reset ok"}; OLED_Show_String(0, 1, 8, reset_ok); break;
        case 0x09 : blu_9_access(); break;   //设备入网错误入口
        default : OLED_Clear_Line(1); uint8_t reset_failed[] = {"reset failed"}; OLED_Clear_Line(1); OLED_Show_String(0, 1, 8, reset_failed); break;
    }
}

static void blu_base(void)
{
    switch(receive_buff[2])
    {
        case 0x00 : OLED_Clear_Line(1); break;
        case 0x01 : OLED_Clear_Line(1); break;
        case 0x02 : break;
        case 0x03 : break;
        case 0x04 : break;
        case 0x05 : break;
        case 0x06 : break;
        case 0x07 : break;
        case 0x08 : break;
        case 0x09 : blu_9_access(); break;
        default : break;
    }
}

static void blu_9_access(void)
{
    uint8_t show[32];
    uint8_t b_id[4];
     
    OLED_Clear_Line(1);
    if(receive_buff[0] == 0x0B)
    {
        uint8_t access_succeed[] = {0x04, 0xC2, 0x00, 0x00, 0x55};
        ctrl_data.b_id[ctrl_data.b_num] = receive_buff[9] + receive_buff[10];

        strcpy((char *)show, "connect id = ");
        strcat((char *)show, (char *)b_id);
        printf("%s\n", show);
        OLED_Show_String(0, 1, 8, show);
        OLED_Show_picture(20, 0, 8, 3);
        ctrl_data.b_num++;
        access_succeed[2] = receive_buff[9]; access_succeed[3] = receive_buff[10];
        UartWrite(WIFI_IOT_UART_IDX_2, access_succeed, sizeof(access_succeed));     //配对成功过，发送一个配对成功的消息给从机
        UartWrite(WIFI_IOT_UART_IDX_0, access_succeed, sizeof(access_succeed));
    }
    else if(receive_buff[0] != 0x0B)
    {
        switch(receive_buff[3])
        {
            case 0xFC: strcpy((char *)show, "not found"); OLED_Show_String(0, 1, 8, show); break;
            case 0xFB: strcpy((char *)show, "0xFB"); OLED_Show_String(0, 1, 8, show); break;
            case 0xFA: strcpy((char *)show, "netkey failed"); OLED_Show_String(0, 1, 8, show); break;
            case 0xF9: strcpy((char *)show, "appkey timeout"); OLED_Show_String(0, 1, 8, show); break;
            case 0xF8: strcpy((char *)show, "appkey failed"); OLED_Show_String(0, 1, 8, show); break;
            default : strcpy((char *)show, "error"); OLED_Show_String(0, 1, 8, show); break;
        }
        return;
    }
}

static void function_b_user(void)
{
    uint8_t access_succeed[] = {0x04, 0xC2, 0x00, 0x00, 0x55};
    switch(receive_buff[6])
    {
        case 0x55 : access_succeed[2] = receive_buff[2]; access_succeed[3] = receive_buff[3];
        UartWrite(WIFI_IOT_UART_IDX_2, access_succeed, sizeof(access_succeed));
        ctrl_data.b_num++; printf("bluetooth connect b_num = %d\n", ctrl_data.b_num); break;     //配对成功后，发送一个配对成功的消息给从机
        case 0x00 : function_b_user_t(); break;    //获取时间
        default : break;
    }
}

static void function_b_user_t(void)
{
    if(server_data.server_connect_state == 1)
    {
        send_buff[0] = 0x0B; send_buff[1] = 0xc2; send_buff[2] = receive_buff[2]; send_buff[3] = receive_buff[3]; send_buff[4] = 0x00;  //3+8=11
        memcpy(send_buff+4, device_data.stime, 9);
        // strcpy((char *)send_buff+4, (char *)device_data.stime);
        UartWrite(WIFI_IOT_UART_IDX_2, send_buff, sizeof(send_buff));
        UartWrite(WIFI_IOT_UART_IDX_0, send_buff, sizeof(send_buff));
    }
    else
    {
        send_buff[0] = 0x0B; send_buff[1] = 0xc2; send_buff[2] = receive_buff[2]; send_buff[3] = receive_buff[3]; send_buff[4] = 0x00;
        send_buff[5] = 0xEE;
        UartWrite(WIFI_IOT_UART_IDX_2, send_buff, sizeof(send_buff));
        UartWrite(WIFI_IOT_UART_IDX_0, send_buff, sizeof(send_buff));
    }
}